package com.xtm.exercise.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.xtm.common.exception.BusinessException;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

public class LocalDateTimeUtils {
    public static LocalDateTime dateLongToLocalDateTime(Long intDate){
        try {
            String date = String.valueOf(intDate);
            DateTime parse = DateUtil.parse(date, "yyyyMMddHHmmss");
            return Instant.ofEpochMilli(parse.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
        }catch (Exception e) {
            throw new BusinessException(-1, String.valueOf(intDate) + "  时间格式错误，请填写为yyyyMMddHHmmss格式");
        }
    }

    public static LocalDateTime getDateTimeOfTimestamp(Long timestamp) {
        if (timestamp == null) {
            return null;
        }
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }
}
